package cn.bdqn.t354.controller;

import cn.bdqn.t354.pojo.Chance;
import cn.bdqn.t354.pojo.Dict;
import cn.bdqn.t354.pojo.User;
import cn.bdqn.t354.service.ChanceService;
import cn.bdqn.t354.service.DictService;
import cn.bdqn.t354.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chance")
public class ChanceController {
    @Resource
    private ChanceService chanceService;
    @Resource
    DictService dictService;
    @Resource
    UserService userService;

    /**
     * 列表展示
     *
     * @param name
     * @param title
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String name, String title,
                       @RequestParam(defaultValue = "1") Integer pageIndex,
                       Model model) {
        //设置每页显示条数
        int pageSize = 3;

        //查询每页显示的数据
        IPage<Chance> chancePager =
                chanceService.getChancesByPage(name,title, pageIndex, pageSize);
        model.addAttribute("chancePager", chancePager);

        //查询条件回显
        model.addAttribute("name",name);
        model.addAttribute("title",title);

        return "chance/list";
    }
    /**
     * 跳转到新增页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(Model model) {
        //查询客户等级
        List<Dict> levelList=dictService.getDictByDictType("客户等级");
        model.addAttribute("levelList",levelList);

        //查询地区
        List<Dict> regionList=dictService.getDictByDictType("地区");
        model.addAttribute("regionList",regionList);

        //查询客户经理
        List<User> userList=userService.getUsersByRoleId(2L);
        model.addAttribute("userList",userList);
        return "chance/add";
    }

    /**
     * 新增操作
     *
     * @param chance
     * @return
     */
    @RequestMapping("/save")
    public String save(Chance chance,HttpSession session) {
        try {
            //获取当前登录用户
            User loginUser=(User)session.getAttribute("loginUser");

            chance.setChcCreateId(loginUser.getUsrId());
            chance.setChcCreateBy(loginUser.getUsrName());
            chance.setChcCreateDate(new Date());

            if(chance.getChcDueId()!=null){
                chance.setChcStatus("2");//已指派
                chance.setChcDueDate(new Date());
            }else{
                chance.setChcStatus("1");//未指派
            }
            chanceService.addChance(chance);
            //跳转到列表页面
            return "redirect:/chance/list";
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到新增页面
            return "redirect:/chance/add";
        }
    }

    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Long id, Model model) {
        //1.根据id查询信息
        Chance chance = chanceService.getChance(id);
        model.addAttribute("chance", chance);
        //2.查询客户等级
        List<Dict> levelList=dictService.getDictByDictType("客户等级");
        model.addAttribute("levelList",levelList);
        //3.查询地区
        List<Dict> regionList=dictService.getDictByDictType("地区");
        model.addAttribute("regionList",regionList);
        //4.查询客户经理
        List<User> userList=userService.getUsersByRoleId(2L);
        model.addAttribute("userList",userList);

        return "chance/edit";
    }

    /**
     * 修改操作
     * @param chance
     * @return
     */
    @RequestMapping("/update")
    public String update(Chance chance) {
        try {
            if(chance.getChcDueId()!=null){
                chance.setChcStatus("2");//已指派
                chance.setChcDueDate(new Date());
            }else{
                chance.setChcStatus("1");//未指派
            }
            chanceService.updateChance(chance);
            //跳转到列表页面
            return "redirect:/chance/list";
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到修改页面
            return "redirect:/chance/edit";
        }
    }
    /**
     * 删除操作
     *
     * @param id
     * @param session
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Long id, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            //查询销售机会
            Chance chance=chanceService.getChance(id);
            //判断销售机会状态,是否为未分配
            if(!chance.getChcStatus().equals("1")){
                map.put("delResult",-1);//销售机会不是未分配，不能删除
                return map;
            }

            //判断当前删除数据的人，是否为销售机会创建人
            //获取当前登录用户
            User loginUser=(User)session.getAttribute("loginUser");
            if(!loginUser.getUsrId().equals(chance.getChcCreateId())){
                map.put("delResult",-2);//删除人不是销售机会创建人，不允许删除
                return map;
            }

            chanceService.deleteChance(id);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }

    /**
     * 跳转指派销售机会页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toAssign")
    public String toAssign(Long id,Model model){
        //1.查询客户经理
        List<User> userList=userService.getUsersByRoleId(2L);
        model.addAttribute("userList",userList);
        //2.根据销售机会ID，查询信息
        Chance chance=chanceService.getChance(id);
        model.addAttribute("chance",chance);

        return "chance/assign";
    }

    /**
     * 保存指派的信息
     * @param chance
     * @return
     */
    @RequestMapping("/assign")
    public String assign(Chance chance){
        chance.setChcDueDate(new Date());
        //修改销售机会的状态
        chance.setChcStatus("2");
        //修改销售机会的指派人员
        chanceService.updateChance(chance);
        try {
            return "redirect:/chance/list";
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到指派页面
            return "redirect:/chance/assign";
        }
    }
}
